/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;
import jeeobserver.HardDiskObserver;
import jeeobserver.utilities.SizeUtilities;

/**
 * The Class HardDiskNotificationRule.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class HardDiskNotificationRule extends HardDiskExclusionRule {

	 /** The Constant serialVersionUID. */
	 private static final long serialVersionUID = 40L;

	 /** The Constant TAG_CODE_CONTEXT. */
	 public static final String TAG_CODE_CONTEXT = "{context}";

	 /** The Constant TAG_CODE_PATH. */
	 public static final String TAG_CODE_PATH = "{path}";

	 /** The Constant TAG_CODE_DISK_USAGE_PERCENTAGE. */
	 public static final String TAG_CODE_DISK_USAGE_PERCENTAGE = "{disk_usage_percentage}";

	 /** The Constant TAG_CODE_DISK_FREE_SPACE_BYTES. */
	 public static final String TAG_CODE_DISK_FREE_SPACE_BYTES = "{disk_free_space_bytes}";

	 /** The Constant TAG_CODE_DISK_FREE_SPACE_FORMATTED. */
	 public static final String TAG_CODE_DISK_FREE_SPACE_FORMATTED = "{disk_free_space_formatted}";

	 /** The Constant TAG_CODE_DISK_USABLE_SPACE_BYTES. */
	 public static final String TAG_CODE_DISK_USABLE_SPACE_BYTES = "{disk_usable_space_bytes}";

	 /** The Constant TAG_CODE_DISK_USABLE_SPACE_FORMATTED. */
	 public static final String TAG_CODE_DISK_USABLE_SPACE_FORMATTED = "{disk_usable_space_formatted}";

	 /** The Constant TAG_CODE_DISK_USED_SPACE_BYTES. */
	 public static final String TAG_CODE_DISK_USED_SPACE_BYTES = "{disk_used_space_bytes}";

	 /** The Constant TAG_CODE_DISK_USED_SPACE_FORMATTED. */
	 public static final String TAG_CODE_DISK_USED_SPACE_FORMATTED = "{disk_used_space_formatted}";

	 /** The Constant TAG_CODE_DISK_TOTAL_SPACE_BYTES. */
	 public static final String TAG_CODE_DISK_TOTAL_SPACE_BYTES = "{disk_total_space_bytes}";

	 /** The Constant TAG_CODE_DISK_TOTAL_SPACE_FORMATTED. */
	 public static final String TAG_CODE_DISK_TOTAL_SPACE_FORMATTED = "{disk_total_space_formatted}";

	 /** The notification. */
	 private Notification notification;

	 /** The send notification only once. */
	 private boolean sendNotificationOnlyOnce;

	 /** The element notificated set. */
	 private static Set<String> elementNotificatedSet = new HashSet<String>();

	 public boolean isObserverInRule(HardDiskObserver observer) {

			boolean result = super.isObserverInRule(observer);

			// id is unique for different devices
			final String idUnique = this.getName() + "[" + observer.getContext() + "|" + observer.getPath() + "]";


			if (result == true) {
				 if (this.sendNotificationOnlyOnce == true) {
						if (HardDiskNotificationRule.elementNotificatedSet.contains(idUnique)) {
							 result = false;
						}
				 }
				 HardDiskNotificationRule.elementNotificatedSet.add(idUnique);
			} else {
				 HardDiskNotificationRule.elementNotificatedSet.remove(idUnique);
			}

			return result;

	 }

	 /**
	  * Instantiates a new hard disk notification rule.
	  *
	  * @param notification the notification
	  */
	 public HardDiskNotificationRule(Notification notification) {
			this.notification = notification;
	 }

	 /**
	  * Gets the notification.
	  *
	  * @param observer the hard disk observer
	  *
	  * @return the notification
	  */
	 public Notification getNotification(HardDiskObserver observer) {

			if (observer != null) {
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_CONTEXT, observer.getContext());
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_PATH, observer.getPath());

				 final DecimalFormat decimalFormat = new DecimalFormat("##0.## %");
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_USAGE_PERCENTAGE, decimalFormat.format(observer.getUsedBytes() / observer.getTotalBytes()));
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_FREE_SPACE_BYTES, observer.getFreeBytes());
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_FREE_SPACE_FORMATTED, SizeUtilities.fromBytesToString(observer.getFreeBytes()));

				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_USED_SPACE_BYTES, observer.getUsedBytes());
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_USED_SPACE_FORMATTED, SizeUtilities.fromBytesToString(observer.getUsedBytes()));

				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_USABLE_SPACE_BYTES, observer.getUsableBytes());
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_USABLE_SPACE_FORMATTED, SizeUtilities.fromBytesToString(observer.getUsableBytes()));

				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_TOTAL_SPACE_BYTES, observer.getTotalBytes());
				 this.notification.setTag(HardDiskNotificationRule.TAG_CODE_DISK_TOTAL_SPACE_FORMATTED, SizeUtilities.fromBytesToString(observer.getTotalBytes()));
			}

			return this.notification;
	 }

	 /**
	  * Gets the notification.
	  *
	  * @return the notification
	  */
	 public Notification getNotification() {
			return this.notification;
	 }

	 /**
	  * Sets the notification.
	  *
	  * @param notification the new notification
	  */
	 public void setNotification(Notification notification) {
			this.notification = notification;
	 }

	 /**
	  * Checks if is send notification only once.
	  *
	  * @return true, if is send notification only once
	  */
	 public boolean isSendNotificationOnlyOnce() {
			return this.sendNotificationOnlyOnce;
	 }

	 /**
	  * Sets the send notification only once.
	  *
	  * @param sendNotificationOnlyOnce the new send notification only once
	  */
	 public void setSendNotificationOnlyOnce(boolean sendNotificationOnlyOnce) {
			this.sendNotificationOnlyOnce = sendNotificationOnlyOnce;
	 }
}
